<?php
/**
 * 《电话服务器状态》实现类
 *
 * @author 孙晓晔
 * @version $Id: Mobile.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/TelUtil.class.php');

$client;

class Asterisk extends TelUtil {
	
	public $cdr = array(
		'-' => '请选择',
		'ns' => '南山',
		'sj' => '沙井',
		'qx' => '清溪',		
	);

	public $orderby = array(
		'-' => '请选择',
		'mobile' => '号段',
		'province' => '省份',
		'city' => '城市',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('cdr_option', $this->cdr);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'tel/asterisk/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		if ($px == '`join`') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['join'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$province_s = $request['province_s'];
		$mobile_s = substr($request['mobile_s'], 0, 7);

		$where = " WHERE 1=1";

		if (!empty($province_s) && $province_s <> '-') {
			$where .= " AND code = '" . $province_s . "'";
		}

		if (!empty($mobile_s)) {
			$where .= " AND mobile = '" . $mobile_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$cdr = $request['cdr_s'];
		if (empty($cdr) || $cdr == '-') {
			$request['cdr_s'] = 'ns';
		}
		
		$data = $this->_abc($controller, $request);
		
		$controller->assign_by_ref('result', $data);
		
		$controller->display($request, 'tel/asterisk/list');
		return true;
	}
	
	function _abc($controller, $request) {
	
		$am = astman($request['cdr_s']);
		$cmd = 'core show channels';
		$ret = $am->Command($cmd);
	
		$data = $ret['data'];
		// echo $data;
		$aa = preg_split("/[\\n]+/i", $data);
	
		$i = 0;
		$ret = array();
		while (list($k, $s) = each($aa)) {
			// echo sprintf("%s\n", $s);
				
			$i++;
			if ($i < 3) {
				continue;
			}
	
			if (preg_match("/(\d+) active channels/i", $s, $m)) {
				continue;
			}
				
			if (preg_match("/(\d+) active call/i", $s, $m)) {
				break;
			}
				
			$a = preg_split("/[\s,]+/", $s);
			$channel = $a[0];
			// print_r($a);
				
			$row = $this->_channel($am, $channel);
			if (!preg_match("/\d+/i", $row['dst'])) {
				continue;
			}
			
			$row['src_t'] = $this->_number($row['src']);
			$row['dst_t'] = $this->_number($row['dst']);
				
			$ret[] = $row;
		}
	
		$am->disconnect();
	
		return $ret;
	}
	
	function _channel($am, $chn) {
		$cmd = sprintf("core show channel %s", $chn);
		// echo $cmd . "\n";
		$ret = $am->Command($cmd);

		$data = $ret['data'];
		// echo $data;
		
		$aa = preg_split("/[\\n]+/i", $data);
		
		$i = 0;
		$ret = array();
		while (list($k, $s) = each($aa)) {
			if (preg_match("/level 1: (\\S+)=(.+)/i", $s, $m)) {
				// print_r($m);
				$ret[$m[1]] = $m[2];
			}
		}		
		
		return $ret;
	}
	
	

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function t($controller, $request) {
		$this->pretreat($controller);

		$s = "13532789657";
		$s = "9118080085263972484";
		$s = "9966880085263972484";

		$a = $this->_number($s);
		
		print_r($a);

		logdate($controller, $request);
		return true;
	}
	
	function ajax_test($controller, $request) {
		error_reporting(0);  
		set_time_limit(0); 
		
		echo "starting...\n";
		
		event_handler('ns');
		// test('ns');
	
		logdate($controller, $request);
		return true;
	}
	
	function ns($controller, $request) {
	
		echo "starting...\n";
	
		$this->event_handler('ns');
	
		logdate($controller, $request);
		return true;
	}
	
	function sj($controller, $request) {
	
		echo "starting...\n";
	
		$this->event_handler('sj');
	
		logdate($controller, $request);
		return true;
	}
	
	function qx($controller, $request) {
	
		echo "starting...\n";
	
		$this->event_handler('qx');
	
		logdate($controller, $request);
		return true;
	}
	
	function wintec($controller, $request) {
	
		echo "starting...\n";
	
		$this->event_handler('wintec');
	
		logdate($controller, $request);
		return true;
	}
	
	function event_handler($sip) {
	
		if (empty($sip)) {
			$sip = 'ns';
		}
		
		global $client, $cfg;
		// tcp://127.0.0.1:1033
		$fmt = "tcp://%s:1033";
		$url = sprintf($fmt, $cfg['workerman']['root']['host']);
		
		// 建立socket连接到内部推送端口
		$client = stream_socket_client($url, $errno, $errmsg, 1,  STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT);
		
		$am = astman($sip);
		$am->Events("call");
		// $am->Events("system,call,log");
		// $am->add_event_handler('newchannel', 'event_newchannel');
		// $am->add_event_handler('unlink', 'event_default');
		// $am->add_event_handler('hangup', array('Asterisk', 'event_hangup'));
		// $am->add_event_handler('hangup', 'event_hangup');
		// $am->add_event_handler('newchannel', array('Asterisk', 'event_newchannel'));
		
		// $am->add_event_handler('newcallerid', 'event_newchannel');
		$am->add_event_handler('newstate', 'event_newstate');
		// $am->add_event_handler('newexten', 'event_newchannel');
		// $am->add_event_handler('link', 'event_newchannel');
		// $am->add_event_handler('bridge', 'event_newchannel');
		// $am->add_event_handler('bridge', 'event_default');
		// $am->add_event_handler('bridge', 'event_default');
		// $am->add_event_handler('status', 'event_default');
		// $am->add_event_handler('cdr', 'event_default');
	
		while (true) {
			// echo "wait response ...\n";
			echo ".";
			$am->wait_response();
		}
	
		$am->disconnect();
	
		logdate($controller, $request);
		return true;
	}
	
	function event_newchannel($e, $a, $host, $port) {
		echo "Asterisk\n";
	
		print_r($a);
	
		echo sprintf("host = %s, port = %s\n", $host, $port);
	}
}

function event_newchannel($event, $data, $host, $port) {
	global $client;
	
	fwrite($client, json_encode($data) . "\n");
	
// 	echo $event . "\n";
// print_r($data);
// 	echo sprintf("host = %s, port = %s\n", $host, $port);
}

function event_newstate($event, $data, $host, $port) {
	global $client;

	fwrite($client, json_encode($data) . "\n");
}

function event_link($event, $data, $host, $port) {
	echo $e . "\n";
	print_r($a);
	echo sprintf("host = %s, port = %s\n", $host, $port);
}

function event_hangup($event, $data, $host, $port) {
	echo $e . "\n";
	print_r($a);
	echo sprintf("host = %s, port = %s\n", $host, $port);
}

?>